﻿#include "CustomTextEdit.h"
#include "styleDefine.h"
#include <QDebug>

CustomTextEdit::CustomTextEdit(QWidget* parent)
    : QFrame(parent)
    , m_bMsgShow(false)
    , m_pInputTextEdit(nullptr)
    , m_pMsgShower(nullptr)
    , m_pAnimationText(nullptr)
    , m_pAnimationMsg(nullptr)
{
    m_pInputTextEdit = new QTextEdit(this);
    m_pMsgShower = new QTextBrowser(this);
#if 0
    m_pInputTextEdit->setStyleSheet("QTextEdit {border-top: 2px solid #EEEEEE; border-left: 2px solid #EEEEEE; border-right: 2px solid #EEEEEE; border-bottom: 2px solid #EEEEEE; border-radius: 2px;}  \
                                     QTextEdit:focus {border-top: 2px solid #EEEEEE; border-left: 2px solid #EEEEEE; border-right: 2px solid #EEEEEE; border-bottom: 2px solid blue;}");
#endif
    m_pAnimationText = new QPropertyAnimation(m_pInputTextEdit, "geometry");
    m_pAnimationText->setDuration(ANIMATION_DURATION_TIME);
    m_pAnimationMsg = new QPropertyAnimation(m_pMsgShower, "geometry");
    m_pAnimationMsg->setDuration(ANIMATION_DURATION_TIME);
}

void CustomTextEdit::resizeEvent(QResizeEvent *event)
{
    reshow(m_bMsgShow);
}

void CustomTextEdit::reshow(const bool bShow)
{
    if(false == bShow)
    {
        if(nullptr != m_pInputTextEdit)
        {
            m_pInputTextEdit->setGeometry(WIDGET_SPACINGS_LEN, WIDGET_SPACINGS_LEN, this->size().width() - 2 * WIDGET_SPACINGS_LEN, this->size().height() - 2 * WIDGET_SPACINGS_LEN);
        }

        if(nullptr != m_pMsgShower)
        {
            m_pMsgShower->setGeometry(WIDGET_SPACINGS_LEN, this->size().height() - WIDGET_SPACINGS_LEN, this->size().width() - 2 * WIDGET_SPACINGS_LEN, 0);
        }
    }
    else
    {
        const uint nShowHeight = (this->size().height() - 3 * WIDGET_SPACINGS_LEN) / 3;
        if(nullptr != m_pInputTextEdit)
        {
            m_pInputTextEdit->setGeometry(WIDGET_SPACINGS_LEN, WIDGET_SPACINGS_LEN, this->size().width() - 2 * WIDGET_SPACINGS_LEN, 2 * nShowHeight);
        }

        if(nullptr != m_pMsgShower)
        {
            m_pMsgShower->setGeometry(WIDGET_SPACINGS_LEN, 2 * (WIDGET_SPACINGS_LEN + nShowHeight), this->size().width() - 2 * WIDGET_SPACINGS_LEN, nShowHeight);
        }
    }
}

void CustomTextEdit::reshowAnimat(const bool bShow)
{
    const uint nShowHeight = (this->size().height() - 3 * WIDGET_SPACINGS_LEN) / 3;
    if(false == bShow)
    {
        m_pAnimationText->setStartValue(QRect(WIDGET_SPACINGS_LEN, WIDGET_SPACINGS_LEN, this->size().width() - 2 * WIDGET_SPACINGS_LEN, 2 * nShowHeight));
        m_pAnimationText->setEndValue(QRect(WIDGET_SPACINGS_LEN, WIDGET_SPACINGS_LEN, this->size().width() - 2 * WIDGET_SPACINGS_LEN, this->size().height() - 2 * WIDGET_SPACINGS_LEN));

        m_pAnimationMsg->setStartValue(QRect(WIDGET_SPACINGS_LEN, 2 * (WIDGET_SPACINGS_LEN + nShowHeight), this->size().width() - 2 * WIDGET_SPACINGS_LEN, nShowHeight));
        m_pAnimationMsg->setEndValue(QRect(WIDGET_SPACINGS_LEN, this->size().height() - WIDGET_SPACINGS_LEN, this->size().width() - 2 * WIDGET_SPACINGS_LEN, 0));
    }
    else
    {
        m_pAnimationText->setStartValue(QRect(WIDGET_SPACINGS_LEN, WIDGET_SPACINGS_LEN, this->size().width() - 2 * WIDGET_SPACINGS_LEN, this->size().height() - 2 * WIDGET_SPACINGS_LEN));
        m_pAnimationText->setEndValue(QRect(WIDGET_SPACINGS_LEN, WIDGET_SPACINGS_LEN, this->size().width() - 2 * WIDGET_SPACINGS_LEN, 2 * nShowHeight));

        m_pAnimationMsg->setStartValue(QRect(WIDGET_SPACINGS_LEN, this->size().height() - WIDGET_SPACINGS_LEN, this->size().width() - 2 * WIDGET_SPACINGS_LEN, 0));
        m_pAnimationMsg->setEndValue(QRect(WIDGET_SPACINGS_LEN, 2 * (WIDGET_SPACINGS_LEN + nShowHeight), this->size().width() - 2 * WIDGET_SPACINGS_LEN, nShowHeight));
    }

    m_pAnimationText->start();
    m_pAnimationMsg->start();
}

void CustomTextEdit::setPlaceholderText(QString strText)
{
    m_pInputTextEdit->setPlaceholderText(strText);
}

void CustomTextEdit::setMsgShow(QString strInfo)
{
    if (nullptr != m_pMsgShower)
    {
        m_pMsgShower->clear();
        m_pMsgShower->setText(strInfo);
    }
}

QString CustomTextEdit::getDocument()
{
    QString strDoc;
    if(nullptr != m_pInputTextEdit)
    {
        strDoc = m_pInputTextEdit->document()->toPlainText();
    }

    return strDoc;
}

void CustomTextEdit::clear()
{
    if(nullptr != m_pInputTextEdit)
    {
        m_pInputTextEdit->clear();
    }

    if (nullptr != m_pMsgShower)
    {
        m_pMsgShower->clear();
    }
}

bool CustomTextEdit::showMsg()
{
    m_bMsgShow = !m_bMsgShow;
    reshowAnimat(m_bMsgShow);
    return m_bMsgShow;
}
